package gui;

import javax.media.NoDataSourceException;
import javax.media.NoPlayerException;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JScrollPane;

import audio.MicrofoneAusenteException;
import chat.ClientListener;
import chat.ClientWorker;

import usuario.Usuario;

import conexao.ClienteRTP;
import conexao.ServidorRTP;
import controle.Contato;
import controle.ControlePortas;
import controle.IniciaConexaoChatWorker;
import controle.SocketControlConstants;

import javax.swing.JCheckBox;

import java.awt.Dimension;
import java.io.IOException;
import java.util.List;

public class ChatVoxGui extends JFrame {

	private static final long serialVersionUID = 1L;
	
	private Contato contato = null;  //  @jve:decl-index=0:
	
	private List<Contato> contatos = null;
	
	private Usuario usuarioSessao = null;

	private JPanel jContentPane = null;

	private JPanel jPanelChat = null;

	private JPanel jPanelChatUsuario = null;

	private JPanel jPanelNomeContato = null;

	private JButton jButtonEnviar = null;

	private JPanel jPanelVolume = null;

	private JTextArea jTextAreaConversa = null;

	private JTextArea jTextAreaMensagemUsuario = null;

	private JSlider jSliderVolume = null;

	private JLabel jLabelMax = null;

	private JLabel jLabelMin = null;

	private JLabel jLabelFigura = null;

	private JTextField jTextFieldContato = null;

	private JScrollPane jScrollPaneConversa = null;

	private JScrollPane jScrollPaneMensagemUsuario = null;
	
	private ClientListener clienteListener = null;
	
	private ClientWorker clienteWorker = null;

	private JPanel jPanelVoip = null;

	private JCheckBox jCheckBox = null;

	private int portaDestino = 0;
	
	private ClienteRTP recepcao = null;  
	
	private ServidorRTP envio = null;  
	
	private String ipDestino = null;  //  @jve:decl-index=0:
	
	
	public String getIpDestino() {
		return ipDestino;
	}
	public void setIpDestino(String ipDestino) {
		this.ipDestino = ipDestino;
	}
	/**
	 * This is the default constructor
	 */
	public ChatVoxGui(List<Contato> contatos, Usuario usuario) {
		super();
		this.contatos = contatos;
		this.usuarioSessao = usuario;
		initialize();
	}
	public void chatVoxGuiConecta(Contato contato, Usuario usuarioSessao) {
		
		this.contato = contato;
		this.usuarioSessao = usuarioSessao;
		this.clienteListener = new ClientListener(this.usuarioSessao.getPortaEntradaChat(), this);
		this.clienteListener.start();
		this.jTextFieldContato.setText(this.contato.getIp() + " - " + this.contato.getIp());
	}

	public void SetTextAreaConversa(String msg) 
	{
		this.jTextAreaConversa.setText(msg);
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(764, 550);
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("LiveVox");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJPanelChat(), null);
			jContentPane.add(getJPanelChatUsuario(), null);
			jContentPane.add(getJPanelNomeContato(), null);
			jContentPane.add(getJButtonEnviar(), null);
			jContentPane.add(getJPanelVolume(), null);
			jContentPane.add(getJPanelVoip(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanelChat	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelChat() {
		if (jPanelChat == null) {
			jPanelChat = new JPanel();
			jPanelChat.setLayout(null);
			jPanelChat.setBounds(new Rectangle(1, 59, 754, 313));
			jPanelChat.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Conversa", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), Color.darkGray));
			jPanelChat.add(getJScrollPaneConversa(), null);
		}
		return jPanelChat;
	}

	/**
	 * This method initializes jPanelChatUsuario	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelChatUsuario() {
		if (jPanelChatUsuario == null) {
			jPanelChatUsuario = new JPanel();
			jPanelChatUsuario.setLayout(null);
			jPanelChatUsuario.setBounds(new Rectangle(2, 373, 579, 146));
			jPanelChatUsuario.setFont(new Font("Dialog", Font.PLAIN, 12));
			jPanelChatUsuario.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED), "Mensagem do usu�rio", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), Color.darkGray));
			jPanelChatUsuario.add(getJScrollPaneMensagemUsuario(), null);
		}
		return jPanelChatUsuario;
	}

	/**
	 * This method initializes jPanelNomeContato	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelNomeContato() {
		if (jPanelNomeContato == null) {
			jLabelFigura = new JLabel();
			jLabelFigura.setBounds(new Rectangle(12, 19, 25, 27));
			jLabelFigura.setDisabledIcon(new ImageIcon(getClass().getResource("/live/Live/imagens/user.png")));
			jLabelFigura.setIcon(new ImageIcon(getClass().getResource("/live/Live/imagens/user.png")));
			jLabelFigura.setText("");
			jPanelNomeContato = new JPanel();
			jPanelNomeContato.setLayout(null);
			jPanelNomeContato.setBounds(new Rectangle(2, 2, 754, 55));
			jPanelNomeContato.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED), "Contato", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), Color.darkGray));
			jPanelNomeContato.add(jLabelFigura, null);
			jPanelNomeContato.add(getJTextFieldContato(), null);
		}
		return jPanelNomeContato;
	}

	/**
	 * This method initializes jButtonEnviar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonEnviar() {
		if (jButtonEnviar == null) {
			jButtonEnviar = new JButton();
			jButtonEnviar.setBounds(new Rectangle(626, 490, 86, 29));
			jButtonEnviar.setText("Enviar!");
			jButtonEnviar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					enviarMensagem();
				}
			});
		}
		return jButtonEnviar;
	}

	protected void enviarMensagem() 
	{
		this.clienteWorker = new ClientWorker(this.contato, this.jTextAreaMensagemUsuario.getText(), this.portaDestino);
		this.clienteWorker.run();
	}
	/**
	 * This method initializes jPanelVolume	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelVolume() {
		if (jPanelVolume == null) {
			jLabelMin = new JLabel();
			jLabelMin.setBounds(new Rectangle(10, 14, 48, 23));
			jLabelMin.setFont(new Font("Dialog", Font.BOLD, 10));
			jLabelMin.setText("Min.");
			jLabelMax = new JLabel();
			jLabelMax.setBounds(new Rectangle(119, 11, 48, 26));
			jLabelMax.setFont(new Font("Dialog", Font.BOLD, 10));
			jLabelMax.setText("     Max.");
			jPanelVolume = new JPanel();
			jPanelVolume.setLayout(null);
			jPanelVolume.setBounds(new Rectangle(584, 418, 171, 64));
			jPanelVolume.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED), "Volume", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), Color.darkGray));
			jPanelVolume.add(getJSliderVolume(), null);
			jPanelVolume.add(jLabelMax, null);
			jPanelVolume.add(jLabelMin, null);
		}
		return jPanelVolume;
	}

	/**
	 * This method initializes jTextAreaConversa	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextAreaConversa() {
		if (jTextAreaConversa == null) {
			jTextAreaConversa = new JTextArea();
			jTextAreaConversa.setEditable(false);
			jTextAreaConversa.setSize(new Dimension(739, 250));
		}
		return jTextAreaConversa;
	}

	/**
	 * This method initializes jTextAreaMensagemUsuario	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextAreaMensagemUsuario() {
		if (jTextAreaMensagemUsuario == null) {
			jTextAreaMensagemUsuario = new JTextArea();
		}
		return jTextAreaMensagemUsuario;
	}

	/**
	 * This method initializes jSliderVolume	
	 * 	
	 * @return javax.swing.JSlider	
	 */
	private JSlider getJSliderVolume() {
		if (jSliderVolume == null) {
			jSliderVolume = new JSlider();
			jSliderVolume.setBounds(new Rectangle(4, 29, 159, 31));
		}
		return jSliderVolume;
	}

	/**
	 * This method initializes jTextFieldCOntato	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldContato() {
		if (jTextFieldContato == null) {
			jTextFieldContato = new JTextField();
			jTextFieldContato.setBounds(new Rectangle(44, 16, 703, 33));
			jTextFieldContato.setEditable(false);
			//jTextFieldContato.setText(this.contato.getNome() + " - " + this.contato.getEmail());
		}
		return jTextFieldContato;
	}

	/**
	 * This method initializes jScrollPaneConversa	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneConversa() {
		if (jScrollPaneConversa == null) {
			jScrollPaneConversa = new JScrollPane();
			jScrollPaneConversa.setBounds(new Rectangle(6, 17, 742, 291));
			jScrollPaneConversa.setEnabled(false);
			jScrollPaneConversa.setViewportView(getJTextAreaConversa());
		}
		return jScrollPaneConversa;
	}

	/**
	 * This method initializes jScrollPaneMensagemUsuario	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneMensagemUsuario() {
		if (jScrollPaneMensagemUsuario == null) {
			jScrollPaneMensagemUsuario = new JScrollPane();
			jScrollPaneMensagemUsuario.setBounds(new Rectangle(7, 19, 568, 121));
			jScrollPaneMensagemUsuario.setViewportView(getJTextAreaMensagemUsuario());
		}
		return jScrollPaneMensagemUsuario;
	}
	
	public void setContatoConversa(Contato contato) {
		// TODO Auto-generated method stub
		this.contato = contato;
		this.jTextFieldContato.setText(this.contato.getNome() + " - " + this.contato.getEmail());
	}
	/**
	 * This method initializes jPanelVoip	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelVoip() {
		if (jPanelVoip == null) {
			jPanelVoip = new JPanel();
			jPanelVoip.setLayout(null);
			jPanelVoip.setBounds(new Rectangle(584, 373, 171, 43));
			jPanelVoip.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED), "Voip", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			jPanelVoip.add(getJCheckBox(), null);
		}
		return jPanelVoip;
	}
	/**
	 * This method initializes jCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox() {
		if (jCheckBox == null) {
			jCheckBox = new JCheckBox("  Habilitar Serviço");
			jCheckBox.setBounds(new Rectangle(10, 16, 153, 21));
			jCheckBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						conectar();
					} catch (NoDataSourceException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NoPlayerException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (MicrofoneAusenteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
		}
		return jCheckBox;
	}
	
	public void conectar() throws NoDataSourceException, NoPlayerException, MicrofoneAusenteException, IOException
	{	
			this.getServidorRTP();
			this.getClienteRTP();	
	}
	
	public void desconectar() throws IOException{
		if(!((this.envio == null)&&(this.recepcao == null))){			
			this.envio.stop();
			this.recepcao.stop();
			this.envio = null;
			this.recepcao = null;
		}
	}
	
	public void initListener(ControlePortas controlePortas)
	{
		this.clienteListener = new ClientListener(controlePortas.getPortaSaida(), this);
		this.portaDestino = controlePortas.getPortaEntrada();
		this.ipDestino = controlePortas.getIpFonte();
		System.out.println("ContatosSize" + contatos.size());
		for(int i = 0 ; i < this.contatos.size(); i++)
		{
			if(this.contatos.get(i).getIp().equals(controlePortas.getIpDestino()))
			{
				this.contato = this.contatos.get(i);
				this.jTextFieldContato.setText(this.contato.getNome() + " - " + "<" + this.contato.getEmail() + ">");
			}
		}
		this.clienteListener.start();
	}
	
	public ClienteRTP getClienteRTP(){
		if(this.recepcao == null){				
			this.recepcao = new ClienteRTP(this.contato.getIp(), 5000);
			this.recepcao.start();
		}
		return this.recepcao;
	}
	
	public ServidorRTP getServidorRTP() throws NoDataSourceException, NoPlayerException, MicrofoneAusenteException, IOException{
		if(this.envio == null){
			this.envio = new ServidorRTP(this.contato.getIp(),5000);
			this.envio.start();
		}
		return this.envio;
	}
	
	public void setContatos(List<Contato> contatos)
	{
		this.contatos = contatos;
	}
	
	public int getPortaDestino() 
	{
		return portaDestino;
	}
	
	public void setPortaDestino(int portaDestino) 
	{
		this.portaDestino = portaDestino;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
